package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo23 {
    public static void main(String[] args) {
        //阻塞队列
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(4);
        Thread t1 = new Thread(() -> {
            int n = 0;
            while (true) {
                try {
                    queue.put(n);
                    System.out.println("消费者生产了"+n);
                    n++;

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "producer");
        Thread t2 = new Thread(() -> {
            while (true) {
                try {
                    Integer n = queue.take();
                    System.out.println("消费者消费了"+n);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "consumer");
        t1.start();
        t2.start();
    }
}
